package com.ld.task;

import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

@Component
public class AsyncTask {

    private Logger logger = Logger.getLogger(AsyncTask.class);

    @Async
    public Future<String> doTask1() throws InterruptedException {

        logger.info("doTask1 started.");
        long start = System.currentTimeMillis();
        logger.info("当前线程的名字："+Thread.currentThread().getName());
        Thread.sleep(3000);
        long end = System.currentTimeMillis();
        logger.info("doTask1 end.time="+(end-start));
        return new AsyncResult<>("task1 executor finished.");
    }

    @Async
    public void doTask2() throws InterruptedException {
        logger.info("doTask2 started.");
        long start = System.currentTimeMillis();
        logger.info("当前线程的名字："+Thread.currentThread().getName());
        Thread.sleep(3000);
        long end = System.currentTimeMillis();
        logger.info("doTask2 end.time="+(end-start));
    }
}
